package AAA.web;


import AAA.exceptions.MoneyNotEnoughException;
import AAA.exceptions.TransferException;
import AAA.service.AccountService;
import AAA.service.impl.AccountServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/transfer")
public class account extends HttpServlet {
    //为了让这个对象在其他方法中也可以使用，声明为实例变量
    private AccountService accountService = new AccountServiceImpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fromActno = req.getParameter("fromActno");
        String toActno = req.getParameter("toActno");
        String money1 = req.getParameter("money");
        double money = Double.parseDouble(money1);
//        System.out.println(money);

        //调用servlet的转账方法完成转账。调业务层
        try {
            accountService.transfer(fromActno,toActno,money);
            //view视图层展示结果
            resp.sendRedirect(req.getContextPath()+"/success.html");
        } catch (MoneyNotEnoughException e) {
            resp.sendRedirect(req.getContextPath()+"/error1.html");
        } catch (TransferException e) {
           resp.sendRedirect(req.getContextPath()+"/error2.html");
        }

    }
}
